/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tarena.lbs.attach.controller;

import com.tarena.lbs.attach.pojo.po.AttachPO;
import com.tarena.lbs.attach.pojo.vo.FileVO;
import com.tarena.lbs.attach.service.AttachService;
import com.tarena.lbs.base.protocol.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Api(value = "attach", tags = "附件中心管理")
@Slf4j
@Validated
@RestController
@RequestMapping("attach")
public class AttachController {

    @Autowired
    private AttachService attachService;
    @Value("${image_path}")
    private String baseStoragePath;

    @Value("${url}")
    private String baseUrl;
    @PostMapping("upload")
    @ApiOperation(value = "上传附件")
    public Result<FileVO> uploadAttachInfo(@RequestPart("file") MultipartFile file) throws Exception {
//        String filename = file.getOriginalFilename();
//        String substring = filename.substring(filename.lastIndexOf("."));
//        String fileUuid = UUID.randomUUID() + substring;
//        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
//        String datePath = "D:/files" + sdf.format(new Date());
//        File dateFile = new File(datePath);
//        if (!dateFile.exists()){
//            dateFile.mkdirs();
//        }
//        String url = datePath + fileUuid;
//        file.transferTo(new File(url));
//        FileVO fileVO = new FileVO();
//        fileVO.setUrl(datePath);
//        fileVO.setFileUuid(fileUuid);
//        log.debug("fileVO的的参数为:{}",fileVO);
//        Result<FileVO> result = new Result<>(fileVO);
//        return result;


        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileUuid = UUID.randomUUID().toString() + extension;

        // 创建日期目录
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String datePath = sdf.format(new Date());

        // 使用配置的存储路径 + 日期目录
        File targetDir = new File(baseStoragePath, datePath);
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        // 保存文件
        File targetFile = new File(targetDir, fileUuid);
        file.transferTo(targetFile);

        // 构建可访问的URL (使用配置的URL前缀)
        // 注意: 静态资源映射到 /static/ 路径
        String accessUrl = baseUrl + datePath + "/" + fileUuid;

        FileVO fileVO = new FileVO();
        fileVO.setUrl(accessUrl);  // 设置完整的访问URL
        fileVO.setFileUuid(fileUuid);

        log.debug("文件上传成功，访问URL: {}", accessUrl);
        return new Result<>(fileVO);

    }
}
